import router from "../../../apis/routers";

const key = getFilePathStore(import.meta.url)

export const useTestItemStore = defineStore(key, {
    state: () => ({
        coreBaseNavightionStore: useCoreBaseNavightionStore(),
        checkBox: -1,
        indexId: 0,
        setItemsKey: 'setTestItems',

        pageViewIndex: 1,
        pageViewPager: 10,

        item: { id: -1, isProxy: -1, data: {} },
        activeItems: [
            { context: 'context1 with data 1' },
            { context: 'context2 with data 2' },
            { context: 'context3 with data 3' },
            { context: 'context4 with data 4' },
            { context: 'context5 with data 5' },
            { context: 'context6 with data 6' },
            { context: 'context7 with data 7' },
            { context: 'context8 with data 8' },
            { context: 'context9 with data 9' },
            { context: 'context10 with data 10' },
        ],
        activeItem: { context: '' }
    }),
    actions: {
        async initStore() {
            this.coreBaseNavightionStore.initStore().then(async (res) => {
                await res.setTitle('测试题目');
                (await (await res.setHaveLeftIcon(true)).setLeftIconName('revoke')).setLeftArrowClick(() => {
                    router.push({ name: 'home' });
                });
                await res.setHaveRightIcon(false);
            });
            this.indexId = 0;
            this.activeItem = this.activeItems[this.indexId];
            await asyncSetCookie(this.setItemsKey, {});
        },
        async getNextPageItem() {
            (this.activeItems.length > 0) ? (() => {
                // TODO: api: 得到下一页数据
                getSomeValue(this.indexId + 1, 9) ?
                    (console.log('得到下一页数据'))
                    : void 0;
            })() : (() => {
                void 0;
            })();
        },
        async getNext() {
            (this.activeItems.length > 0 && this.checkBox != -1) ? (() => {
                asyncGetCookie(this.setItemsKey).then(async (cookie) => {
                    this.item.id = this.indexId;
                    this.item.isProxy = this.checkBox;
                    this.item.data = this.activeItems[this.indexId];
                    cookie[this.activeItem.context] = this.item;
                    asyncSetCookie(this.setItemsKey, cookie).then(async () => {
                        await this.getNextPageItem();
                        this.indexId = (this.indexId + 1) % this.activeItems.length;
                        this.activeItem = this.activeItems[this.indexId];
                        this.checkBox = -1;
                    })
                })
            })() : (() => { })();
        },
        async getBefore() {
            (this.activeItems.length > 0) ? (() => {
                this.indexId = (this.indexId - 1) & this.activeItems.length;
                this.activeItem = this.activeItems[this.indexId];
            })() : void 0;
        },
        async getCount() {
            (this.activeItems.length > 0) ? (() => {
                asyncGetCookie(this.setItemsKey).then(async (item) => {
                    console.log(item);
                    router.push({name:'testResult'})
                })
            })() : void 0;
        }
    },
    persist: {
        key: key,
        storage: sessionStorage
    }
})